package com.moqianfan.mcuassistant.activity;

import android.support.annotation.IdRes;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.BaseFragmentAdapter;
import com.moqianfan.mcuassistant.fragment.BaseFragment;
import com.moqianfan.mcuassistant.fragment.learn.LearnFragment;
import com.moqianfan.mcuassistant.fragment.more.MineFragment;
import com.moqianfan.mcuassistant.fragment.play.PlaysFragment;
import com.moqianfan.mcuassistant.fragment.tools.ToolsFragment;
import com.moqianfan.mcuassistant.view.CustomViewPager;
import com.moqianfan.mcuassistant.view.MessageDialog;
import com.umeng.analytics.MobclickAgent;

import net.youmi.android.nm.sp.SpotManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.vp_main)
    CustomViewPager vpMain;
    @BindView(R.id.radio_nav_bottom_menu)
    RadioGroup radioMain;

    private MessageDialog dialog;

    @Override
    public void findViews() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initViews() {
        List<BaseFragment> mFragments = new ArrayList<>();
        mFragments.add(new LearnFragment());
        mFragments.add(new ToolsFragment());
        mFragments.add(new PlaysFragment());
        mFragments.add(new MineFragment());

        BaseFragmentAdapter mAdapter = new BaseFragmentAdapter(getSupportFragmentManager(), mFragments);
        vpMain.setCurrentItem(0);
        vpMain.setOffscreenPageLimit(3);
        vpMain.setAdapter(mAdapter);
        radioMain.setOnCheckedChangeListener(this);
    }

    @Override
    public void initDatas() {

    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                MobclickAgent.onKillProcess(this);
                android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
                System.exit(0);
                finish();
            }
        }
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.nav_bottom_menu_1:
                vpMain.setCurrentItem(0);
                break;
            case R.id.nav_bottom_menu_2:
                vpMain.setCurrentItem(1);
                break;
            case R.id.nav_bottom_menu_3:
                vpMain.setCurrentItem(2);
                break;
            case R.id.nav_bottom_menu_4:
                vpMain.setCurrentItem(3);
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SpotManager.getInstance(this).onAppExit();
        if (null != dialog)
            dialog = null;
    }
}
